home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / fread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.4 KB  |  68 lines

  1. /* nothing like the origonal from
  2.  * from Dale Schumacher's dLibs
  3.  */
  4.  
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <assert.h>
  11. #include <string.h>
  12. #include <memory.h>
  13.  
  14. size_t  fread(data, size, count, fp)
  15. register void *data;
  16. size_t size;
  17. size_t count;
  18. register FILE *fp;
  19. {
  20.     register unsigned long n;
  21.     register long l, cnt;
  22.     register unsigned int f;
  23.     
  24.     assert((data != NULL));
  25.     assert((size != 0));
  26.     
  27.     f = fp->_flag;
  28.     if(f & _IORW) f = (fp->_flag |= _IOREAD);
  29.     if(!(f & _IOREAD) || (f & (_IOERR | _IOEOF)))
  30.     return(0);
  31.     
  32.     l = 0;
  33.     n = (unsigned long)count * (unsigned long)size;
  34.     assert((n <= LONG_MAX));
  35.   again:    
  36.     if((cnt = fp->_cnt) > 0)
  37.     {
  38.     cnt = (cnt < n) ? cnt : n;
  39.     bcopy(fp->_ptr, data, cnt);
  40.     fp->_cnt -= cnt;
  41.     fp->_ptr += cnt;
  42.     l += cnt;
  43.     data += cnt;
  44.     n -= cnt;
  45.     }
  46.     /* n == how much more */
  47.     if(n > 0)
  48.     {
  49.     if(n < fp->_bsiz)
  50.     { /* read in fp->_bsiz bytes into fp->_base and do it again */
  51.         fp->_ptr = fp->_base;
  52.         if((cnt = lread(fp->_file, fp->_base, (long)fp->_bsiz)) <= 0)
  53.         {   /* EOF or error */
  54.         fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  55.         goto ret;
  56.         }
  57.         fp->_cnt = cnt;
  58.         goto again;
  59.     }
  60.     else
  61.     { /* read in n bytes into data */
  62.         l += lread(fp->_file, data, (long)n);
  63.     }
  64.     }
  65.   ret:
  66.     return((l > 0) ? (size_t)((unsigned long)l / (unsigned long)size) : 0);
  67. }
  68.